#!/usr/bin/python
#  -*- coding: utf-8 -*-
# http://www.mobanma.com/store/index/p/1.html
import gzip
import json
import random

import urllib2
from StringIO import StringIO


def GetUserAgent():
    UserAgent = [
        'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.0.1471.914 Safari/537.36',
        'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
        'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
        'Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1',
        'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9b4) Gecko/2008030317 Firefox/3.0b4',
        'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.36 Safari/525.19',
        'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
        'Mozilla/5.0 (Linux; U; Android 5.1.1; zh-CN; MI NOTE Pro Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.4.0.921 Mobile Safari/537.36'
    ]
    return UserAgent[random.randrange(0, len(UserAgent) - 1 )]

def GET(URL):
    try:
        request = urllib2.Request(URL)
        request.add_header('User-Agent', GetUserAgent())
        request.add_header('Accept-Encoding', 'gzip, deflate, br')
        request.add_header('Accept', '* / *')
        response = urllib2.urlopen(request,data="{'method': 'showBGS1103', 'tag':tag, 'index':options, 'pagesize':pagesize}",timeout=15)
        if response.info().get('Content-Encoding') == 'gzip':
            buf = StringIO(response.read())
            f = gzip.GzipFile(fileobj=buf)
            html_doc = f.read()
        else:
            html_doc = response.read()
        return html_doc
    except Exception as e:
        print e.message
        html_doc = ""
        return html_doc
TYPE = [
    {'name': 'BGS1106','maxage': '38','data':'method=showBGS1106&tag=STS1011&pagesize=18'},
    {'name': 'BGS1102','maxage': '44','data':'method=showBGS1102&tag=STS1011&pagesize=20'},
    {'name': 'BGS1101','maxage': '53','data':'method=showBGS1101&tag=STS1011&pagesize=16'},
    {'name': 'BGS1103','maxage': '63','data':'method=showBGS1103&tag=STS1011&pagesize=16'}
]

for T in TYPE:
    i = 1
    while i < T['maxage']:
        test_data = T['data'] + "&index=" + str(i)
        # test_data_urlencode = urllib.urlencode(test_data)
        requrl = "http://xxx.xx.com"
        req = urllib2.Request(url=requrl, data=test_data)
        res_data = urllib2.urlopen(req)
        res = json.loads(res_data.read())
        for img in res:
            try:
                request = urllib2.Request("http://xxx.xx.com" + img['tempqr_showimg'])
                picture = urllib2.urlopen(request).read()
                local = open(T['name']+"\\" + img['tempqr_showimg'].replace('/imgs/', ''), 'wb')
                local.write(picture)
                local.close()
                print "Save " + img['tempqr_showimg'].replace('/imgs/', '')
            except:
                pass

        i = i + 1
        print "Over page: " + i

